<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: sap<songaiping@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 线上家博会-商品模型
 */

class OnlineShopGoodsModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('activity_id', 'isActivity', '必须选择活动', self::MUST_VALIDATE, 'function', self::MODEL_BOTH),
        array('online_goods_name', 'require', '商品名称不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('online_goods_name', 'checkName', '商品名称已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('online_goods_name', '1,30', '商品名称长度不能超过30个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
        array('online_goods_pic', 'gtNumber', '请上传图片', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
        array('market_price', 'double', '市场价格必须为数字', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('market_price', '0', '市场价格必须是大于0的数字', self::MUST_VALIDATE, 'notequal', self::MODEL_BOTH),
        array('activity_price', 'double', '活动价格必须为数字', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
//         array('activity_price', '0', '活动价格必须是大于0的数字', self::MUST_VALIDATE, 'notequal', self::MODEL_BOTH),
    	array('sort', 'number', '排序必须为数字', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('sort', '1,5', '排序长度不能大于5个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_BOTH),
    	array('status', '1', self::MODEL_INSERT),
    );

    /**
     * 检测名称是否存在
     * @return boolean          ture - 未重名，false - 已重名
     */
    protected function checkName($online_goods_name){
    	$online_goods_id = (int)I('online_goods_id');
    	//判断是否存在
    	$map = array();
    	if($online_goods_id != 0){
    		$map['online_goods_id'] = array('neq',$online_goods_id);
    	}
    	$map['online_goods_name'] = $online_goods_name;
    	$map['status'] = array('gt',-1);
    	if($this->where($map)->find()){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 验证大于0的数字
     */
    public function gtNumber($online_goods_pic){
        $online_goods_pic = (int)$online_goods_pic;
        if($online_goods_pic <= 0){
            return false;
        }
    }
    
    /**
     * @todo 新增或更新一个商品
     * @author sap
     * @since 2016-3-9
     */
    public function update(){
        /* 获取数据对象 */
    	$data = $this->create($_POST);
        if(empty($data)){
            return false;
        }
        if(!D('OnlineShop')->onlineShopShow((int)$data['online_shop_id'])){
            $this->error = '店铺不存在';
            return false;
        }
        /* 添加或更新数据 */
        if(empty($data['online_goods_id'])){ //新增数据
            $online_goods_id = $this->add();
            if(!$online_goods_id){
                $this->error = '新增商品出错！';
                return false;
            }
            action_log('online_shop_goods_add', 'online_shop_goods', $online_goods_id, UID);
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新商品出错！';
                return false;
            }else{
            	$online_goods_id = $data['online_goods_id'];
            	action_log('online_shop_goods_edit', 'online_shop_goods', $online_goods_id, UID);
            }
        }
        //内容添加或更新完成
        return $online_goods_id;
    }

    /**
     * @todo 商品详情
     * @param string $field 查询字段
     * @return false|array
     * @author sap
     */
    public function onlineShopGoodsShow($online_goods_id=0,$field=''){
    	$online_goods_id = (int)$online_goods_id;
    	$map = array();
    	if($online_goods_id != 0){
    	    $map['online_goods_id'] = $online_goods_id;
    	}
    	if(empty($map)){
    	    return null;
    	}
    	$map['status'] = array('gt',-1);
    	return $this->where($map)->field($field)->find();
    }
    
    /**
     * @todo 商品列表
     * @param array $where 查询条件数组
     * @param string $field 查询字段
     * @param string $orderby 排序
     * @author sap
     */
    public function getOnlineShopGoodsList($where = array(),$field= '',$orderby='sort desc,online_goods_id desc'){
    	return $this->where($where)->field($field)->order($orderby)->select();
    }

}
